home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / DOCZ16.ZIP;1 / DOCZ.LIF / STRMCPY.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-04-24  |  3.4 KB  |  129 lines

  1. ifdef DOCUMENTATION
  2. ; ******************************* DOCZ Header ********************************
  3. .MODULE             strmcpy
  4. .LIBRARY            csub
  5. .TYPE                function
  6. .APPLICATION        string
  7. .SYSTEM             msdos
  8. .SYSTEM             vms
  9. .SYSTEM             unix
  10. .AUTHOR             Software Toolz
  11. .DESCRIPTION
  12.     Perform a bounded string copy and produce a NULL terminated string
  13. .ARGUMENTS
  14.     char *strmcpy(dest,source,maxchar)
  15.         char    *dest,                    /* (w) destination string */
  16.                 *source;                /* (r) source string */
  17.         int    maxchar;                /* (r) maximum characters to copy */
  18. .NARRATIVE
  19.     Strmcpy behaves exactly like strncpy() except that the destination string
  20.     is always NULL-terminated.  The "maxchar" argument specifies the size of
  21.     the destination array (NOT including the NULL).  If the length of the source
  22.     string exceeds "maxchar" then the last character copied to the destination
  23.     string will be a NULL character.
  24. .RETURNS
  25.     The address of the destination string.
  26. .COMMENTS
  27.     This function eliminates a common C pitfall caused by strncpy() producing
  28.     un-terminated strings.
  29. .LANGUAGE
  30.     VMS, MSDOS:  Assembly; UNIX:    C
  31. .FIXES                4/15/88
  32.     Changed documentation to match performance of function.
  33.     Used to be:  The "maxchar" argument specifies the size of the destination
  34.         array (including the NULL).
  35.     Is now: The "maxchar" argument specifies the size of the destination
  36.         array (NOT including the NULL).
  37. .ENDOC                END DOCUMENTATION
  38. ; ****************************************************************************
  39. endif
  40.  
  41.     INCLUDE \HEADER\C.MAC
  42.  
  43. SFRAME    STRUC
  44.     SAV_DI    DW ?
  45.     SAV_SI    DW ?
  46.     SAV_CX    DW ?
  47.     SAV_ES    DW ?
  48.     REG_BP    DW ?        ; base pointer to be pushed
  49.     RTN_ADD DW ?        ; offset of return address
  50.     DEST    DW ?        ; destination string address
  51.     SOURCE    DW ?        ; source string address
  52.     MAXCHAR DW ?        ; character count value
  53. SFRAME    ENDS
  54.  
  55. ; ****************************************************************************
  56. ; code
  57. ; ****************************************************************************
  58.     PSEG                ; begin program section
  59.     CFEXT strlen
  60.  
  61.     CFUN    strmcpy
  62.  
  63.     PUSH    BP
  64.     PUSH    ES
  65.     PUSH    CX
  66.     PUSH    SI
  67.     PUSH    DI
  68.     MOV    BP,SP
  69.     MOV    AX,DS
  70.     MOV    ES,AX
  71.     CLD
  72.  
  73.     PUSH    [BP].SOURCE
  74.     CCALL strlen            ; get length of source string
  75.     MOV    SP,BP
  76.     MOV    CX,[BP].MAXCHAR
  77.     INC    AX        ; add space for NULL
  78.     CMP    AX,CX
  79.     JG SCPY         ; then truncate
  80.     MOV    CX,AX        ; use length as loop counter
  81. SCPY:
  82.     MOV    SI,[BP].SOURCE
  83.     MOV    DI,[BP].DEST
  84.     REP MOVSB            ; do the copy
  85.     XOR    AX,AX        ; load NULL
  86.     STOSB
  87. FINI:
  88.     MOV    AX,[BP].DEST        ; copy destination string address
  89.     POP    DI
  90.     POP    SI
  91.     POP    CX
  92.     POP    ES
  93.     POP    BP
  94.     RET
  95.  
  96.     CFEND strmcpy
  97.  
  98.     ENDPS            ; end program section
  99.  
  100.     END
  101.  
  102. /*****************************************************************************
  103.     C test program
  104. *****************************************************************************/
  105. #include <stdio.h>
  106. #include "csub.h"
  107.  
  108. main(argc,argv)
  109.     int    argc;
  110.     char    *argv[];
  111. {
  112.     char    buff[128];
  113.  
  114.     if (argc == 3)
  115.     {
  116.         strmcpy(buff,argv[1],atoi(argv[2]));
  117.         printf("input string length = %d\r\n",strlen(argv[1]));
  118.         printf("input string = (%s)\r\n",argv[1]);
  119.         printf("maximum characters = %d\r\n",atoi(argv[2]));
  120.         printf("result = (%s)\r\n",buff);
  121.     }
  122.     else
  123.         puts("STRMCPY [string] [max. characters]");
  124. }
  125.  
  126. ; ****************************************************************************
  127. ; End STRMCPY.ASM
  128. ; ****************************************************************************
  129.